package club.zhcs.captrue.module.auth;

import java.util.List;

import org.nutz.dao.entity.Record;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import club.zhcs.capture.bean.auth.User;
import club.zhcs.capture.bean.auth.User.Status;
import club.zhcs.capture.biz.auth.IUserService;
import club.zhcs.capture.biz.dept.ISubsidiaryService;
import club.zhcs.capture.vo.Consts.PermissionGroup;
import club.zhcs.capture.vo.Consts.SessionKeys;

import com.dgj.nutz.module.base.AbstractBaseModule;
import com.dgj.shiro.utils.MD5Utils;
import com.dgj.utils.entries.Result;
import com.dgj.utils.web.pager.Pager;

@At("/user")
public class UserModule extends AbstractBaseModule {

	@Inject
	IUserService userService;
	
	@Inject
	ISubsidiaryService subsidiaryService;

	@Override
	public String _getNameSpace() {
		return PermissionGroup.USER_ADMIN.getNameSpace();
	}

	@At
	@Ok("vel:user/list.html")
	public Result list(int page) {
		page = _fixPage(page);
		Pager<User> pager = userService.listByPage(page);
		pager.setUrl(_base() + "/user/list");
		return Result.success().addData("pager", pager);
	}

	@At
	@Ok("vel:user/list.html")
	public Result search(int page, String key) {
		page = _fixPage(page);
		key = _fixSearchKey(key);
		Pager<User> pager = userService.searchByKey(page, key);
		pager.setUrl(_base() + "/user/search");
		pager.addParas("key", key);
		return Result.success().addData("pager", pager);
	}

	@At
	@GET
	@Ok("vel:user/add.html")
	public Result add() {
		return Result.success().addData("subsidiaries", subsidiaryService.listAll());
	}

	@At
	@POST
	public Result add(@Param("..") User user) {
		if (null != userService.findByName(user.getName())) {
			return Result.fail("用户 '" + user.getName() + "' 已经存在");
		}
		user.setPassword(MD5Utils.MD5(user.getPassword(), "DGJ", 1024));
		user = userService.addUser(user);
		return user == null ? Result.fail("添加用户失败") : Result.success().addData("user", user);
	}

	@At
	public Result delete(int id) {
		return userService.deleteUser(id) ? Result.success() : Result.fail("删除用户失败!");
	}

	@At
	public Result toggleStatus(int id, Status status) {
		User user = new User();
		user.setId(id);
		user.setStatus(status);
		return userService.update(user, "status");
	}

	@At("/info/*")
	@Ok("vel:user/detail.html")
	public Result detail(int id, @Param("..") User user) {
		user = userService.findById(id);
		return Result.success().addData("user", user);
	}

	@At
	@Ok("vel:user/card.html")
	public Result card(@Attr(SessionKeys.SESSION_USER_KEY) User user) {
		return Result.success().addData("user", user);
	}

	@At("/edit/*")
	@Ok("vel:user/edit.html")
	public Result edit(int id, @Param("..") User user) {
		user = userService.findById(id);
		return Result.success().addData("user", user);
	}

	@At
	@POST
	public Result update(@Param("..") User user) {
		return userService.update(user, new String[] { "phone", "email" });
	}

	@At("/setRole/*")
	@GET
	@Ok("vel:user/set_role.html")
	public Result setRole(int id) {
		List<Record> records = userService.findUserRoleStatusByUserId(id);
		return Result.success().addData("records", records).addData("userId", id);
	}

	@At
	@POST
	public Result setRole(@Param("ids") int[] ids, int userId) {
		return userService.setRole(ids, userId);
	}

	@At("/setPermission/*")
	@GET
	@Ok("vel:user/set_permission.html")
	public Result setPermission(int id) {
		List<Record> record = userService.findUserPermissionByUserId(id);
		return Result.success().addData("record", record).addData("userId", id);
	}

	@At
	@POST
	public Result setPermission(@Param("ids") int[] ids, int userId) {
		return userService.setPermission(ids, userId);
	}

}
